define(["jquery","bbcare","comm/datatable/datatable"],function($,$bb,$datatable) {
    var servers = {};
    var URL = $bb.baseUrl("blood");

    /**
     * 查询建档患者
     */
    servers.ajaxArchivePatient = function(params,callback) {
        $bb.ajax({
            url: URL + "patientInfo/queryArchivePatient",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                callback(data,params);
            }
        });
    }

    /**
	 * 查询录入者
	 */
	servers.ajaxCreatAuthor = function(callback){
		$bb.ajax({
			url: URL + "../followapp/ctrl/ptmanage/qryCreatAuthor",
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询录入者出错！");
				}	
			}
		});
	}


    /**
     * 查询方案信息
     */
    servers.ajaxAllScheme = function(params,callback) {
        $bb.ajax({
            url: URL + "scheme/queryAllScheme",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "查询方案信息出错！");
                }
            }
        });
    }

    /**
     * 给患者应用某个治疗方案
     */
    servers.ajaxApplyTreatScheme = function(params,callback) {
        $bb.ajax({
            url: URL + "ptDisPlan/applyTreatScheme",
            data: JSON.stringify({
                data: params
            }),
            success: function(data) {
                if (data.success) {
                    callback(data.data);
                } else {
                    $bb.toastShow(data.msg ? data.msg : "给患者应用某个治疗方案出错！");
                }
            }
        });
    }

    /**
     * 入参
     */
    var inParams = function(args){
        console.log(args)
        args.columns.push({
            "data":"createAuthor",
            "name":"",
            "searchable":true,
            "orderable":false,
            "search":{
            "value":"",
            "regex":false
            }
        })
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "queryCriteria":{
                    "queueId":JSON.parse(_config).queueId,
                    "schemeId":"",
                    "classId": "",
                    "fuzzyVar":$.trim($("#datatableForm input[name=fuzzyVar]").val()),
                    "createDate":$("#datatableForm input[name=createDate]").val(),
                    "createAuthor": $('#createAuthor').val()
                }
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnGender = function(e){
        return e.gender == "F" ? "女" : e.gender == "M" ? "男" : null;
    }
    var columnHisNo = function(e){
        return e.hisNo ? e.hisNo : e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : "暂无";
    }
    var columnPlanStatus = function(e){
        return e.planStatus == "0" ? "正常" : e.planStatus == "01" ? "已完成" : e.planStatus == "02" ? "复发失效" : e.planStatus == "03" ? "已终止" : "";
    }
    var columnsDoctorName = function(e){
        return e.doctor_name ? e.doctor_name : "患者自助";
    }

    var columnsData = function(){
        var columnsArr = [
            {"data":'userName'},
            {"data":columnGender},
            {"data":'birthday'},
            {"data":columnHisNo},
            {"data":'phoneNo'},
            {"data":'age'},
            {"data":'schemeName'},
            {"data":'createDate'},
            {"data":columnsDoctorName},
            {"data":columnPlanStatus},
            {
                "data":null,
                "className":"operate"
            },

        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        // 操作
        if( !$.isEmptyObject(data) ){
            var h = '<div class="operate-box">'+
                    '   <div class="btn-solid btn-solid-green lookInfo">查看资料</div>'+
                    '   <div class="btn-solid btn-solid-orange viewPlan">治疗计划</div>'+
                    '   <div class="btn-solid btn-solid-blue specialtyBags">专科袋</div>'+
                    '</div>';
            $('td.operate', row).html(h);
            // 查看治疗
            $("td.operate .operate-box > div.lookInfo",row).unbind("click");
            $("td.operate .operate-box > div.lookInfo",row).click(function(){
                if(JSON.parse(_config).bch_user == 1){
                    $.extend(data,{"busiTypeList":$bb.getPageParams().busiTypeList});
                    $bb.openPage('bch_user',data).busiTypeList;
                }else{
                    $bb.openPage('bld_lookInfo',data);
                }

            });
            // 治疗计划
            $("td.operate .operate-box > div.viewPlan",row).unbind("click");
            $("td.operate .operate-box > div.viewPlan",row).click(function(){
                $bb.openPage('bld_treatPlan',data);
            });
            // 专科袋
            $("td.operate .operate-box > div.specialtyBags",row).unbind("click");
            $("td.operate .operate-box > div.specialtyBags",row).click(function(){
                $bb.openPage('bld_specialtyBags',data);
            });
        }else{
            $('td.operate', row).html(null);
        }
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        // 搜索 
        $("#serchBtn").click(function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数 
        });
        // 初始化所有工具提示
        $(function(){$("[data-toggle='tooltip']").tooltip();});
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "patientInfo/queryTreatPatient?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

    return servers;
})